import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_navigation/get_navigation.dart';
import 'package:lyf_shop/router/route_conifg.dart';
import 'package:lyf_shop/utils/string_util.dart';
import 'package:lyf_shop/views/login/widget/login_button.dart';
import 'package:lyf_shop/views/login/widget/login_effect.dart';
import 'package:lyf_shop/views/login/widget/login_input.dart';
import 'package:lyf_shop/widget/appbar.dart';

class LYFRegisterPage extends StatefulWidget {
  const LYFRegisterPage({super.key});

  @override
  State<LYFRegisterPage> createState() => _RegisterPageState();
}

class _RegisterPageState extends State<LYFRegisterPage> {

  bool protect = false;
  bool loginEnable = false;
  String? userName;
  String? password;
  String? rePassword;
  String? imoocId;
  String? orderId;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar("注册", "登录",(){
          Get.toNamed(LYFRouteConfig.login);
      }),
      body: Container(
        child: ListView(//自适应键盘弹起，防止遮挡
          children: [
            LoginEffect(protect: protect),
            LoginInput("用户名", "请输入用户名",onChanged: (text) {
              userName = text;
              checkInput();
            },),
            LoginInput("密码", "请输入密码",obscureText: true, onChanged: (text) {
                password = text;
                checkInput();
            },foucsChanged: (focus) {
              this.setState(() {
                protect = focus;
              });
            },),
            LoginInput("确认密码", "请再次输入密码",obscureText: true, onChanged: (text) {
                rePassword = text;
                checkInput();
            },foucsChanged: (focus) {
              this.setState(() {
                protect = focus;
              });
            },),
            LoginInput("慕课网ID", "请输入慕课网ID",keyboardType: TextInputType.number,onChanged: (text){
              imoocId = text;
              checkInput();
            },),
            LoginInput("订单号", "请输入课程订单号后四位", keyboardType: TextInputType.number,onChanged: (text){
              orderId = text;
              checkInput();
            }),
            Padding(
              padding: EdgeInsets.only(top: 40,left: 20, right: 20),
              child: LoginButton(
                  "注册",
                  enable: loginEnable,
                  onPressed: checkParams
              ),
            )
          ],
        ),
      ),
    );
  }

  void checkInput(){
    bool enable = false;

    if(isNotEmpty(userName!) && isNotEmpty(password!) && isNotEmpty(rePassword!) && isNotEmpty(imoocId!) && isNotEmpty(orderId!)) {
      enable = true;
    }else{
      enable = false;
    }

    setState(() {
      loginEnable  = enable;
    });
  }

  void checkParams() {
    String tips = "";
    if(password != rePassword) {
      tips = "两次密码不一致";
    }else if (orderId?.length != 4) {
      tips = "请输入订单号的后四位";
    }

    if(tips != null) {
      print(tips);
      return;
    }

    send();
  }

  void send() {

  }
}
